package com.wgt.strategy;



/**
 * 冒泡排序
 * ASC 升序排序
 * DESC 升序排序
 */
public class DataSorter {

    public static final int ASC = 1;
    public static final int DESC = 2;

    public static void sortArr(int[] datas,int type){
        if (type==ASC){
            ASCSort(datas);
        }else if (type== DESC){
            DESCSort(datas);
        }else{
            throw new IllegalArgumentException("the sort type not found!");
        }
    }
    
    private static void ASCSort(int[] datas){
        for(int i=datas.length;i>0;i--){
            for (int j=0;j<i-1;j++){
                if (datas[j]>datas[j+1]){
                    swap(datas,j,j+1);
                }
            }
        }
    }

    private  static  void DESCSort(int[]datas){
        for (int i=0;i<datas.length;i++){
            for (int j=i;j>0;j--){
                if (datas[j]>datas[j-1]){
                    swap(datas,j,j-1);
                }
            }
        }
    }

    private static void swap(int[] datas, int x, int y) {
        int temp = datas[x];
        datas[x] = datas[y];
        datas[y] = temp;
    }
}
